package com.jin.webvideo.externalInterfaces.video;

import com.jin.webvideo.base.util.CollectionsUtil;
import com.jin.webvideo.exception.UnavailableServiceException;
import com.jin.webvideo.externalInterfaces.PlatformSupport;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.externalInterfaces.entity.FilmographyInfo;

import com.jin.webvideo.externalInterfaces.qqvideo.QQBaseInfoHandle;
import com.jin.webvideo.util.base.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class QQApi extends AbstractVideoAPI{

    @Autowired
    private QQBaseInfoHandle baseInfoHandle;

    @Override
    public Filmography baseInfo(String docid, String tvid) throws UnavailableServiceException {
        try {
            return baseInfoHandle.baseInfo(docid, tvid);
        } catch (IOException e) {
            e.printStackTrace();
            throw new UnavailableServiceException();
        }
    }

    @Override
    public FilmographyInfo getActive(Filmography filmography, List<FilmographyInfo> infos, String tvid) throws UnavailableServiceException {
        FilmographyInfo info = super.getActive(filmography, infos, tvid);
        if (null != info){
            return info;
        }
        return CollectionsUtil.first(infos, temp -> -1 != temp.getUrl().indexOf(filmography.getId()));
    }

    @Override
    public Filmography baseInfoWithVideoInfoList(String docid, String tvid, Integer pageNo, Integer pageSize) throws UnavailableServiceException {
        if (!StringUtil.isEmpty(tvid)){
            return super.baseInfoWithVideoInfoList(docid, tvid, pageNo, pageSize);
        }
        Filmography filmography = baseInfo(docid, tvid);
        List<FilmographyInfo> filmographyInfos = videoInfoList(docid, tvid, pageNo, pageSize);
        filmography.setVideoInfos(filmographyInfos);
        return filmography;
    }

    @Override
    public List<FilmographyInfo> videoInfoList(String docid, String tvid, Integer pageNo, Integer pageSize) throws UnavailableServiceException {
        try {
            return baseInfoHandle.videoInfos(docid, tvid, 0, pageSize);
        } catch (IOException e) {
            e.printStackTrace();
            throw new UnavailableServiceException();
        }
    }

    @Override
    String platformName() {
        return PlatformSupport.PLATFORM_QQ;
    }
}
